package com.hedl.studyandjava8.java8_2unit.java8_25;

import com.hedl.studyandjava8.java8_2unit.java8_25.entity.Apple;
import com.hedl.studyandjava8.java8_2unit.java8_25.entity.Color;
import com.hedl.studyandjava8.java8_2unit.java8_25.service.AppleFormatter;
import com.hedl.studyandjava8.java8_2unit.java8_25.service.ApplePredicate;
import com.hedl.studyandjava8.java8_2unit.java8_25.service.impl.AppleSimpleFormatterImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Big Watermelon
 * @description
 * @date 2024/02/19/16:58
 */
public class TestMain {

    public static List<?> filterApples(List<?> inventory, ApplePredicate p){
        if (inventory.size() == 0){
            throw new RuntimeException("集合为null");
        }
        List<Apple> resultList = new ArrayList<>();
        for (Object o : inventory) {
            if (o instanceof Apple){
                Apple apple = (Apple) o;
                if (p.test(apple)){
                    resultList.add(apple);
                }
            }
        }
        return resultList;
    }

    public static void prettyPrintApple(List<?> inventory, AppleFormatter f){
        if (inventory.size() == 0){
            throw new RuntimeException("集合为null");
        }
        for (Object o : inventory) {
            if (o instanceof Apple){
                Apple apple = (Apple) o;
                String output = f.accept(apple);
                System.out.println(output);
            }
        }
    }

    public static void main(String[] args) {
        Apple apple = new Apple();

        List<Apple> inventory = new ArrayList<>();
        inventory.add(new Apple(100, Color.RED));
        inventory.add(new Apple(12,Color.GREEN));
        inventory.add(new Apple(150,Color.RED));
        inventory.add(new Apple(50,Color.RED));
        inventory.add(new Apple(180,Color.RED));
        inventory.add(new Apple(200,Color.GREEN));
        inventory.add(new Apple(59,Color.GREEN));
        inventory.add(new Apple(160,Color.RED));
        inventory.add(new Apple(110,Color.GREEN));

        //List<?> objects = filterApples(inventory, new AppleRedAndHeavyPredicate());
        //System.out.println(objects);

        //prettyPrintApple(inventory,new AppleFancyFormatterImpl());
        prettyPrintApple(inventory,new AppleSimpleFormatterImpl());
    }
}
